﻿using HalconDotNet;
using LD.BC;
using LD.Host;
using LD.Mes.Sharp;
using LD.Mes;
using LD.Tool;
using LD.项目;
using PubSub;
using ReaLTaiizor.Util;
using System;
using System.Collections.Generic;
using System.Data.Entity.Core.Metadata.Edm;
using System.Linq;
using System.Reflection;
using System.Reflection.PortableExecutable;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using LD.Host.WIP;
using LD.Vision;

namespace LD.Tool
{
    [Tool(Text = "FOBAOI视觉", FrmParam = "ToolFobAoiCCDFrm", ToolType = ToolEnum.项目)]
    public class ToolFobAoiCCD : BaseTool
    {
        private TB_Tool _tool;
        public override OpResult Execute(TB_Tool model)
        {
            try
            {
                _tool = model;
                stopwatch.Start();
                if (model.Param != null)
                {
                    //移至Mark1
                    TB_Point mark1 = Appbus.PartNo.Config.AxisPoint.Where(x => x.Name == "Mark1").FirstOrDefault();
                    TB_Point mark2 = Appbus.PartNo.Config.AxisPoint.Where(x => x.Name == "Mark2").FirstOrDefault();
                    if (mark1 == null || mark2 == null)
                        Log(_tool, "找不到拍照位", LogLevel.警告);
                    else
                    {
                        isSuccess = OffsetR(mark1, mark2);
                        if (isSuccess)
                            isSuccess = OffsetXY(mark1, mark2);
                    }
                }
                if (!isSuccess)
                    Log(model, $"视觉矫正失败", LogLevel.警告);
            }
            catch (Exception ex)
            {
                Log(model, ex.Message, LogLevel.严重);
            }
            finally
            {
                //关闭光源
                BcFactory.Instance.SetOut("光源", false);
                stopwatch.Stop();
            }
            return new OpResult() { IsSuccess = isSuccess, Message = message, ExecuteTime = stopwatch.ElapsedMilliseconds };
        }
        private bool OffsetR(TB_Point mark1, TB_Point mark2)
        {
            bool result = false;
            //移动位置
            mark1.Move(true);
            //拍照1
            CameraFactory.Instance.CameraList[""].Trigger();
            //模板匹配
            //TemplateHelper.FindTemplate(null,
            //                            CameraFactory.Instance.CameraList[""].Image,
            //                            CameraFactory.Instance.CameraList[""].ShapeModels[""]);
            //移至Mark2
            mark1.Move(true);
            //拍照2
            CameraFactory.Instance.CameraList[""].Trigger();
            //模板匹配

            /*计算偏移R
             * 
            */
            return result;
        }
        private bool OffsetXY(TB_Point mark1, TB_Point mark2)
        {
            bool result = false;
            //移动位置
            mark1.Move(true);
            //拍照1
            CameraFactory.Instance.CameraList[""].Trigger();
            //模板匹配
            //TemplateHelper.FindTemplate(null,
            //                            CameraFactory.Instance.CameraList[""].Image,
            //                            CameraFactory.Instance.CameraList[""].ShapeModels[""]);
            //移至Mark2
            mark1.Move(true);
            //拍照2
            CameraFactory.Instance.CameraList[""].Trigger();
            //模板匹配

            /*获取基准值
             *计算偏移R
            */
            return result;
        }
    }
}
